<?php


namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_team\Entity\ShusyTeam;
use Drupal\shusy_team_apply\Entity\ShusyTeamApply;

/**
 * @OA\Schema(
 *   schema="MyInnerTeam",
 *   type="object",
 *   description="内部团队",
 * )
 */
class MyInnerTeam extends AbstractBaseModel {

  protected function getEntityType() {
    return 'shusy_team';
  }

  protected function setValue() {
    if ($this->entity instanceof ShusyTeam) {
      $uid = $this->getUid();
      $type = $this->getType($uid, $this->entity);
      $teamApply = $this->getTeamApply($this->entity);
      $value = [
        'id' => (int) $this->entity->id,
        /**
         * @OA\Property(
         *   property="type",
         *   type="integer",
         *   description="关系 1 自己 2 市场高级顾问 3 市场总监 4 市场部长 5 分红股东",
         * ),
         */
        'type' => (int) $type['type'],
        /**
         * @OA\Property(
         *   property="typeLabel",
         *   type="string",
         *   description="关系",
         * ),
         */
        'typeLabel' => (string) $type['label'],
        /**
         * @OA\Property(
         *   property="name",
         *   type="string",
         *   description="申请人姓名",
         * ),
         */
        'name' => (string) $teamApply['name'],
        /**
         * @OA\Property(
         *   property="phone",
         *   type="string",
         *   description="申请人电话",
         * ),
         */
        'phone' => (string) $teamApply['phone'],
        /**
         * @OA\Property(
         *   property="user",
         *   ref="#/components/schemas/User",
         *   description="下级用户",
         * ),
         */
        'user' => User::get($this->entity->uid),
      ];
      $this->merge($value);
    }
  }

  protected function getTeamApply(ShusyTeam $shusyTeam) {
    $es = shusy_team_apply_load_multiple(FALSE, ['uid' => $shusyTeam->uid]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyTeamApply) {
        //状态 0 审核中 1 已通过 2 未通过
        return ['name' => $e->name, 'phone' => $e->phone];
      }
    }
    return ['name' => '', 'phone' => ''];
  }

  protected function getType(int $uid, ShusyTeam $shusyTeam) {
    //类型 1 自己 2 市场高级顾问 3 市场总监 4 市场部长 5 分红股东
    $type = 0;
    $label = '未知';
    if ($shusyTeam->uid == $uid) {
      return [
        'type' => 1,
        'label' => '自己',
      ];
    }
    if ($shusyTeam->market_1 == $uid) {
      $type = 2;
      $label = '市场高级顾问';
    }
    if ($shusyTeam->market_2 == $uid) {
      $type = 3;
      $label = '市场总监';
    }
    if ($shusyTeam->company_1 == $uid) {
      $type = 4;
      $label = '市场部长';
    }
    if ($shusyTeam->company_2 == $uid) {
      $type = 5;
      $label = '分红股东';
    }
    return [
      'type' => $type,
      'label' => $label,
    ];
  }
}
